﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RedBlueBalls
{
    class RedDigit
    {
        private static bool analyzed = false;
        private static bool checkBest = false;
        private static HashSet<int> lastDigits = new HashSet<int>();

        public static void analyze(RedBlueBallRecord[] records)
        {
            if (checkBest)
            {
                int[] setSizes = new int[7];
                for (int i = 0; i < 7; i++)
                    setSizes[i] = 0;

                foreach (RedBlueBallRecord record in records)
                {
                    setSizes[record.GetDigits().Count]++;
                }

                for (int i = 0; i < 7; i++)
                    Tracer.AppendMessage(String.Format("Digit Set Size {0}: {1}", i, setSizes[i]));
            }

            lastDigits = records[records.Length - 1].GetDigits();
            analyzed = true;
        }

        public static bool predict(RedBlueBallRecord candidate)
        {
            if (!analyzed)
            {
                Tracer.AppendMessage("Please analyze the data first!");
                return false;
            }

            HashSet<int> digits = candidate.GetDigits();
            if (digits.Count < 4)
                return false;

            // Must have shared digits with last record but no more than 3
            digits.IntersectWith(lastDigits);
            if (digits.Count == 0 || digits.Count > 3)
                return false;

            return true;
        }
    }
}
